package online.lona.mc;

import online.lona.mc.bukkit.plugin.ymer.Ymer;

public class NativeInterface {
    public static final String CHANNEL_NAME = "ymer";
    public static final String VERSION = "1.1.11";
    private static boolean isLibraryLoaded = false;

    static {
        if (!isLibraryLoaded) {
            if (EnvUtil.is64Bits()) {
                if (EnvUtil.isWindows()) {
                    System.loadLibrary("libymer64");
                } else if (EnvUtil.isLinux()) {
                    System.load(System.getProperty("user.dir") + "/" + Ymer.Ymer.PLUGIN_ROOT + "/libiconv.so");
                    System.load(System.getProperty("user.dir") + "/" + Ymer.Ymer.PLUGIN_ROOT + "/libmbedcrypto.so");
                    System.load(System.getProperty("user.dir") + "/" + Ymer.Ymer.PLUGIN_ROOT + "/libmbedx509.so");
                    System.load(System.getProperty("user.dir") + "/" + Ymer.Ymer.PLUGIN_ROOT + "/libmbedtls.so");
                    System.load(System.getProperty("user.dir") + "/" + Ymer.Ymer.PLUGIN_ROOT + "/libz.so");
                    System.load(System.getProperty("user.dir") + "/" + Ymer.Ymer.PLUGIN_ROOT + "/libymer.so");
                } else {
                    System.out.println("不支持操作系统: " + System.getProperty("os.name"));
                }
            } else {
                if (EnvUtil.isWindows()) {
                    System.loadLibrary("libymer32");
                } else if (EnvUtil.isLinux()) {
                    System.out.println("不支持操作系统: " + System.getProperty("os.name"));
                } else {
                    System.out.println("不支持操作系统: " + System.getProperty("os.name"));
                }
            }
        }
    }

    private static NativeInterface instance = null;

    public static NativeInterface getInstance() {
        return instance;
    }

    private NativeInterface() {

    }

    public static NativeInterface serverInit() {
        instance = new NativeInterface();
        new Thread(new Runnable() {
            @Override
            public void run() {
                instance.initAsServer();
            }
        }).start();
        return instance;
    }

    public static NativeInterface clientInit(final YmerServer server) {
        instance = new NativeInterface();
        new Thread(new Runnable() {
            @Override
            public void run() {
                instance.initAsClient(server);
            }
        }).start();
        return instance;
    }

    private native void initAsServer();

    private native void initAsClient(YmerServer server);

    public native void VerClient(YmerPlayer player);

    public native void onPlayerJoinGame(YmerPlayer player);

    public native void onPlayerQuit(YmerPlayer player);

    /**
     * 当客户端收到服务端发送的消息时
     *
     * @param message
     * @return
     */
    public native byte[] onServerMessage(byte[] message);

    /**
     * 当服务端收到客户端发送的消息时
     *
     * @param message
     * @param player
     */
    public native void onClientMessage(byte[] message, YmerPlayer player);

}
